<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>检测函数或代码运行时长</title>
</head>

<body>
  <button>点击运行时长</button>
  <script>
    function add() {
      for (var i = 0; i <= 10; i++) {
        console.log(i)
      }
    }
    //开始测试并输出
    function test() {
      var start = new Date().getTime();
      add();
      var end = new Date().getTime();
      return (end - start) + "ms";
    }
    var time = test(add);
    alert(time);
  </script>
</body>

</html>